/*
 * Copyright (C) 2009-2018 Hangzhou 2Dfire Technology Co., Ltd. All rights reserved
 */
package com.ws.soa.consumer.service;

import com.google.common.base.Preconditions;
import com.ws.soa.consumer.dao.mapper.WorksLaudMapper;
import com.ws.soa.consumer.dao.mapper.WorksMapper;
import com.ws.soa.consumer.domain.WorksLaudDO;
import com.ws.soa.consumer.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * WorksLaudService
 *
 * @author luobosi@2dfire.com
 * @since 2019-03-06
 */
@Service
public class WorksLaudService {

    @Resource
    private WorksLaudMapper worksLaudMapper;

    @Resource
    private WorksService worksService;


    public boolean save(String userId, String workId) {
        Preconditions.checkArgument(StringUtils.isNoneEmpty(userId));
        Preconditions.checkArgument(StringUtils.isNoneEmpty(workId));

        this.worksService.increaseLaudNum(workId);


        return this.worksLaudMapper.insert(this.packageDO(userId, workId)) > 0;
    }

    public List<WorksLaudDO> findAllByWorkId(String workId) {
        Preconditions.checkArgument(StringUtils.isNoneEmpty(workId));

        return this.worksLaudMapper.findAllByWorkId(workId);
    }

    private WorksLaudDO packageDO(String userId, String workId) {
        WorksLaudDO worksLaudDO = new WorksLaudDO();
        worksLaudDO.setId(UUIDUtil.getUUID());
        worksLaudDO.setUserId(userId);
        worksLaudDO.setWorkId(workId);

        return worksLaudDO;
    }
}
